//
//  LocationPermission.m
//  YuanXinSystemPermissions
//
//  Created by 晏德智 on 2017/2/8.
//  Copyright © 2017年 晏德智. All rights reserved.
//

#import "LocationPermission.h"
#import <CoreLocation/CoreLocation.h>
#import "PermissionType.h"

@interface LocationPermission() <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager* locationManager;
@property (copy) void (^completionHandler)(NSString *);
@end

@implementation LocationPermission

+ (NSString *)getStatus
{
    int status = [CLLocationManager authorizationStatus];
    switch (status) {
        case kCLAuthorizationStatusAuthorizedAlways:
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            return RNPStatusAuthorized;
        case kCLAuthorizationStatusDenied:
            return RNPStatusDenied;
        case kCLAuthorizationStatusRestricted:
            return RNPStatusRestricted;
        default:
            return RNPStatusUndetermined;
    }
}

- (void)request:(NSString*)type completionHandler:(void (^)(NSString *))completionHandler
{
    NSString *status = [LocationPermission getStatus];
    if (status == RNPStatusUndetermined || status == RNPStatusDenied) {
        self.completionHandler = completionHandler;
        
        if (self.locationManager == nil) {
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self;
        }
        
        if ([type isEqualToString:@"always"]) {
            [self.locationManager requestAlwaysAuthorization];
        } else {
            [self.locationManager requestWhenInUseAuthorization];
        }
    } else {
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse && [type isEqualToString:@"always"]) {
            completionHandler(RNPStatusDenied);
        } else {
            completionHandler(status);
        }
    }
}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status != kCLAuthorizationStatusNotDetermined) {
        if (self.locationManager) {
            self.locationManager.delegate = nil;
            self.locationManager = nil;
        }
        
        if (self.completionHandler) {
            //for some reason, checking permission right away returns denied. need to wait a tiny bit
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                self.completionHandler([LocationPermission getStatus]);
                self.completionHandler = nil;
            });
        }
    }
}

@end
